Refactored code to not be as messy.