refactoring and code tidying up