move key handling into separate file