Minor refactoring and tidying up