Fix key pair generation bug if previous keys do not exist