refactored to clean up code