Refactored to clean up code