Fixing bug with duplicate detection