code reorg and cleanup