Are you just trying to find the key with the highest value and revert it?  Might want to rename keyToUndo to latestChange.