Clear[red] red[l_]:=Module[{v={},i,t,j}, Print["Length[l] ",Length[l]]; For[i=1,i<=Length[l],++i,( t=l[[i]]; For[j=1,j<=Length[v],++j,t=Expand[t-v[[j]]*Coefficient[t,Last[v[[j]]]]]]; If[!(t===0),(t=Expand[t/(Last[t] /. {OY[___]->1, ON[___]->1})])]; If[!(t===0),AppendTo[v,t]]; )]; Print["Length[v] ",Length[v]]; Print["Last /@ v ",Last /@ v]; v ]