Structure of Optimal Codes