refactored and much neater