More refactoring and cleaning up mainly