Code Cleanup and restructuring