Reorganize code

Pulling working code back into master.