more tidying and refactoring