refactor key generation