Fix duplicates by replacing $.each with for-in.