Updated key generation logic