Refactored non-DRY code