Code cleanup and reorganization