Refactoring code and cleaning up a lot of hack jobs