Refactor duplicate detection