Major refactoring and clean up. I really need to break these into smaller commits