Refactored code to clean up a bit