improved key handling