Added null check for keypairs