Further reorg of code and bug fixes