Cleanup 23oct1728

Refactors a ton of code.
Fixes some small bugs.