Refactored path detection method