Use key function to fix bug and avoid needlessly removing and re-adding elements.