Refactored code and removed unused code but still ugly.