Refactoring out some duplication and some formatting tidying