make generate_key more efficient