Bug fixes and reorg