Cleaned up and refactored