reorganize code and other misc
more code reorg