refactor logic