Checking for duplicates with improvements.