fix undo index going out of bounds in some cases