Improved keyboard function