Moved the key handling into its own function.

Takes the address of the variable so as not to keep making new
variables.