code cleanup and restructuring