Extracted keybuffer code