refactored & tidied