code cleanup and reorganization