Refactored detection logic to helper