Rewrite undo function.