major code cleanup and reorg