Refactored messy code