refactored code to allow for more readability and smaller methods