Improvements to key handling