code tidy up and refactor