Cleaned up code and refactored