Refactor detection logic.