some more unit tests & minor code reorg