Refined key input handling and fixed a few bugs.