reorg of code and cleanup