Some minor refactoring and code tidying