More minor refactoring and code tidy up.