slight refactoring and tidying of code