restructured entire code