Refactor logic