A better way handling duplicate keys

Thanks as always StackOverflow :P