working version; detects duplicates