lots of cleaning and refactoring