Big cleanup, little bit of refactoring