Refactoring and tidy-up