Some extensive code reorg