Refactored, simplified and removed unused code.