fix the undo algo