Fix some bugs in undo