Fix bug in undo code causing null pointer exception