Refactored code to clean up