cleaned up and refactored