Big clean up and refactoring