code reorg and tests