Fixed handling of duplicate IDs