Fixed error when handling duplicate keys