change order of index and begin/end flag and swap begin/endflags value to