cleaned up and refactored code