Various bits of tidying and refactoring