simplify key handling code