Fixed bug where undo could corrupt files