Big code cleanup
More code cleanup and reorg