Refactored duplicate detection