Add history and clear commands, refactor handleKeyUp function