improve undo code