Optimizations to key handling