tidied/refactored code