refactor the logic.