Improved key handling