Some fixes and cleanups to the undo/redo code