Big cleanup and refactor