Code cleanup and restructuring