Refactored code and fixed a lot of issues.