Some tidying and refactoring