some minor refactoring and code tidying