improve keypair handling