-c write (iout,*) "ishift",ishift," ires",ires,
-c & " ires_old",ires_old
- ibeg=0
+! write (iout,*) "ishift",ishift," ires",ires,&
+! " ires_old",ires_old
+ ibeg=0
+ else if (ibeg.eq.2) then
+! Start a new chain
+ ishift=-ires_old+ires-1 !!!!!
+ ishift1=ishift1-1 !!!!!
+! write (iout,*) "New chain started",ires,ishift,ishift1,"!"
+ ires=ires-ishift+ishift1
+ ires_old=ires
+ ibeg=0