factor out keygen