Make key handling more typesafe

The code was correct before, but this is more idiomatic.