move key creation into separate function