code cleanup and reorg