tidy up and general refactoring