refactoring and tidy up