massive clean up and refactoring