refactoring long methods and cleaning up some misc stuff