refactored some code to be neater, make more logical sense when read