anchor.diff:  %% takes single list from expression and the start function and returns 
   %% {SPrev,S, SPos,SNext,EPrev,E, EPos,ENext, W} 
   %% can only be tumbling with no end function
  +windowclause([], _, _) -> [];
   windowclause(L, StartFun, WType) ->
  -   case add_position(L) of
  -      [] -> [];
  -      L1 ->
  -         Bw = winstart([[]|L1], StartFun, []),
  -         %?dbg("BW",Bw),
  -         Bw2 = lists:map(fun(B) ->
  -                               L2 = element(9, B),
  -                               S = ?seq:from_list(L2),
  -                               case xqerl_types:instance_of(S, WType) of
  -                                  #xqAtomicValue{value = true} ->
  -                                     setelement(9, B, S);
  -                                  _ ->
  -                                     ?err('XPTY0004')
  -                               end
  -                         end, Bw),
  -         lists:reverse(Bw2)
  -   end.
  +   L1 = add_position(L),
  +   Bw = winstart([[]|L1], StartFun, []),
  +   flatten_window_return(Bw, WType).
   
   %% takes single list from expression and the start/end functions and returns 
   %% {SPrev,S, SPos,SNext,EPrev,E, EPos,ENext, W} 
   %% Type is tumbling or sliding
  +windowclause([], _, _, _, _) -> [];
   windowclause(L, StartFun, EndFun, {Type, Only}, WType) ->
  -   case add_position(L) of
  -      [] -> [];
  -      L1 ->
  -         Bw = winstart([[]|L1], StartFun, EndFun, Type, Only),
  -         %?dbg("BW",Bw),
  -         Bw2 = lists:map(fun(B) ->
  -                               L2 = element(9, B),
  -                               S = ?seq:from_list(L2),
  -                               case xqerl_types:instance_of(S, WType) of
  -                                  #xqAtomicValue{value = true} ->
  -                                     setelement(9, B, S);
  -                                  _ ->
  -                                     ?err('XPTY0004')
  -                               end
  -                         end, Bw),
  -         lists:reverse(Bw2)
  -   end;
  +   L1 = add_position(L),
  +   Bw = winstart([[]|L1], StartFun, EndFun, Type, Only),
  +   flatten_window_return(Bw, WType);
   windowclause(L, StartFun, EndFun, Type, WType) ->
      windowclause(L, StartFun, EndFun, {Type, false}, WType).
   
  -
  -% tumbling window with no end function, means window as of each 'true'
  +flatten_window_return(Bw, WType) ->
  +   [begin
  +       L2 = element(9, B),
  +       S = xqerl_seq3:from_list(L2),
  +       case xqerl_types:instance_of(S, WType) of
  +          #xqAtomicValue{value = true} ->
  +             setelement(9, B, S);
  +          _ -> 
  +             ?err('XPTY0004')
  +       end
  +    end || B <- Bw, B =/= []].
  +
  +
  +% tumbling window with no end function, means window as if each 'true'
  +winstart([], _, _) -> [];
  +winstart([{S, SPos}], StartFun, _) -> 
  +   case bool(StartFun({S,SPos,[],[]})) of
  +      true ->
  +         [{S,SPos,[],[],S,SPos,[],[],[S]}];
  +      _ ->
  +         []
  +   end;
  +winstart([[],{S, SPos}], StartFun, []) ->
  +   case bool(StartFun({S,SPos,[],[]})) of
  +      true ->
  +         winstart([{S, SPos}], StartFun, {S,SPos,[],[],S,SPos,[],[],[S]});
  +      _ ->
  +         winstart([{S, SPos}], StartFun, [])
  +   end;
   winstart([[],{S, SPos}] = L, StartFun, Acc) ->
      case bool(StartFun({S,SPos,[],[]})) of
         true ->
  -         case Acc of
  -            [] ->
  -               winstart(tl(L), StartFun, {S,SPos,[],[],S,SPos,[],[],[S]});
  -            A ->
  -               R = lists:reverse(element(9, A)),
  -               winstart(L, StartFun, []) ++ [setelement(9, A, R)]
  -         end;
  -      _ ->
  -         case Acc of
  -            [] ->
  -               winstart(tl(L), StartFun, []);
  -            {S1,SPos1,SPrev1,SNext1,_,_, _,_,W} ->
  -               NewAcc = {S1,SPos1,SPrev1,SNext1,S,SPos,[],[],[S|W]},
  -               winstart(tl(L), StartFun, NewAcc)
  -         end
  +         R = lists:reverse(element(9, Acc)),
  +         [setelement(9, Acc, R) | winstart(L, StartFun, [])];
  +      _ ->
  +         {S1,SPos1,SPrev1,SNext1,_,_, _,_,W} = Acc,
  +         NewAcc = {S1,SPos1,SPrev1,SNext1,S,SPos,[],[],[S|W]},
  +         winstart([{S, SPos}], StartFun, NewAcc)
  +   end;
  +winstart([[],{S, SPos},{SNext, _}|_] = L, StartFun, []) ->
  +   case bool(StartFun({S,SPos,[],SNext})) of
  +      true ->
  +         winstart(tl(L), StartFun, {S,SPos,[],SNext,S,SPos,[],SNext,[S]});
  +      _ ->
  +         winstart(tl(L), StartFun, [])
      end;
   winstart([[],{S, SPos},{SNext, _}|_] = L, StartFun, Acc) ->
      case bool(StartFun({S,SPos,[],SNext})) of
         true ->
  -         case Acc of
  -            [] ->
  -               winstart(tl(L), StartFun, {S,SPos,[],SNext,S,SPos,[],SNext,[S]});
  -            A ->
  -               R = lists:reverse(element(9, A)),
  -               winstart(L, StartFun, []) ++ [setelement(9, A, R)]
  -         end;
  -      _ ->
  -         case Acc of
  -            [] ->
  -               winstart(tl(L), StartFun, []);
  -            {S1,SPos1,SPrev1,SNext1,_,_, _,_,W} ->
  -               NewAcc = {S1,SPos1,SPrev1,SNext1,S,SPos,[],SNext,[S|W]},
  -               winstart(tl(L), StartFun, NewAcc)
  -         end
  -   end;
  +         R = lists:reverse(element(9, Acc)),
  +         [setelement(9, Acc, R) | winstart(L, StartFun, [])];
  +      _ ->
  +         {S1,SPos1,SPrev1,SNext1,_,_, _,_,W} = Acc,
  +         NewAcc = {S1,SPos1,SPrev1,SNext1,S,SPos,[],SNext,[S|W]},
  +         winstart(tl(L), StartFun, NewAcc)
  +   end;
  +% last call , returns
  +winstart([{SPrev,_},{S, SPos}], StartFun, []) ->
  +   case bool(StartFun({S,SPos,SPrev,[]})) of
  +      true ->
  +         [{S,SPos,SPrev,[],S,SPos,SPrev,[],[S]}];
  +      _ ->
  +         []
  +   end;
  +% last call , returns
   winstart([{SPrev,_},{S, SPos}], StartFun, Acc) ->
      case bool(StartFun({S,SPos,SPrev,[]})) of
         true ->
  -         case Acc of
  -            [] ->
  -               [{S,SPos,SPrev,[],S,SPos,SPrev,[],[S]}];
  -            A ->
  -               R = lists:reverse(element(9, A)),
  -               [{S,SPos,SPrev,[],S,SPos,SPrev,[],S} , setelement(9, A, R)]
  -         end;
  -      _ ->
  -         case Acc of
  -            [] ->
  -               [];
  -            {S1,SPos1,SPrev1,SNext1,_,_, _,_,W} ->
  -               [{S1,SPos1,SPrev1,SNext1,S,SPos,SPrev,[],lists:reverse([S|W])}]
  -         end
  +         R = lists:reverse(element(9, Acc)),
  +         [setelement(9, Acc, R),{S,SPos,SPrev,[],S,SPos,SPrev,[],S}];
  +      _ ->
  +         {S1,SPos1,SPrev1,SNext1,_,_, _,_,W} = Acc,
  +         [{S1,SPos1,SPrev1,SNext1,S,SPos,SPrev,[],lists:reverse([S|W])}]
  +   end;
  +winstart([{SPrev,_},{S, SPos},{SNext, _}|_] = L, StartFun, []) ->
  +   case bool(StartFun({S,SPos,SPrev,SNext})) of
  +      true ->
  +         winstart(tl(L), StartFun, {S,SPos,SPrev,SNext,S,SPos,SPrev,SNext,[S]});
  +      _ ->
  +         winstart(tl(L), StartFun, [])
      end;
   winstart([{SPrev,_},{S, SPos},{SNext, _}|_] = L, StartFun, Acc) ->
      case bool(StartFun({S,SPos,SPrev,SNext})) of
         true ->
  -         case Acc of
  -            [] ->
  -               winstart(tl(L), StartFun, 
  -                        {S,SPos,SPrev,SNext,S,SPos,SPrev,SNext,[S]});
  -            A ->
  -               R = lists:reverse(element(9, A)),
  -               winstart(L, StartFun, []) ++ [setelement(9, A, R)]
  -         end;
  -      _ ->
  -         case Acc of
  -            [] ->
  -               winstart(tl(L), StartFun, []);
  -            {S1,SPos1,SPrev1,SNext1,_,_, _,_,W} ->
  -               NewAcc = {S1,SPos1,SPrev1,SNext1,S,SPos,SPrev,SNext,[S|W]},
  -               winstart(tl(L), StartFun, NewAcc)
  -         end
  +         R = lists:reverse(element(9, Acc)),
  +         [setelement(9, Acc, R) | winstart(L, StartFun, [])];
  +      _ ->
  +         {S1,SPos1,SPrev1,SNext1,_,_, _,_,W} = Acc,
  +         NewAcc = {S1,SPos1,SPrev1,SNext1,S,SPos,SPrev,SNext,[S|W]},
  +         winstart(tl(L), StartFun, NewAcc)
      end.
   
   
  
positive.code: 
  
  winend(SPrev,S,SPos,SNext,[[],{E, EPos},{ENext, _}|_] = L, EndFun, Acc, Only) ->
     case bool(EndFun({S,SPos,SPrev,SNext,E,EPos,[],ENext})) of
        true ->
           {[{S,SPos,SPrev,SNext,E,EPos,[],ENext, ?seq:append(E, Acc)}], tl(L)};
        _ ->
           winend(SPrev,S,SPos,SNext,tl(L), EndFun, ?seq:append(E, Acc), Only)
     end;
  winend(SPrev,S,SPos,SNext,[[],{E, EPos}] = L, EndFun, Acc, Only) ->
     case bool(EndFun({S,SPos,SPrev,SNext,E,EPos,[],[]})) of
        true ->
           {[{S,SPos,SPrev,SNext,E,EPos,[],[], ?seq:append(E, Acc)}], tl(L)};
        _ ->
           winend(SPrev,S,SPos,SNext,tl(L), EndFun, ?seq:append(E, Acc), Only)
     end;
  winend(SPrev,S,SPos,SNext,[{EPrev,_},{E, EPos}], EndFun, Acc, Only) ->
     case bool(EndFun({S,SPos,SPrev,SNext,E,EPos,EPrev,[]})) of
        true ->
           {[{S,SPos,SPrev,SNext,E,EPos,EPrev,[], ?seq:append(E, Acc)}], []};
        _ ->
           if Only == true ->
                 {[], []};
              true ->
                 {[{S,SPos,SPrev,SNext,E,EPos,EPrev,[], ?seq:append(E, Acc)}], []}
           end
     end;
  winend(SPrev,S,SPos,SNext,[{EPrev,_}], EndFun, Acc, Only) ->
     case bool(EndFun({S,SPos,SPrev,SNext,[],[],EPrev,[]})) of
        true ->
           {[{S,SPos,SPrev,SNext,[],[],EPrev,[], Acc}], []};
        _ ->
           if Only == true ->
                 {[], []};
              true ->
                 {[{S,SPos,SPrev,SNext,[],[],EPrev,[], Acc}], []}
           end
     end;
  winend(SPrev,S,SPos,SNext,[{EPrev,_},{E, EPos},{ENext, _}|_] = L, 
         EndFun, Acc, Only) ->
     case bool(EndFun({S,SPos,SPrev,SNext,E,EPos,EPrev,ENext})) of
        true ->
           {[{S,SPos,SPrev,SNext,E,EPos,EPrev,ENext, ?seq:append(E, Acc)}], 
            tl(L)};
        _ ->
           winend(SPrev,S,SPos,SNext,tl(L), EndFun, ?seq:append(E, Acc), Only)
     end.
  
  %% functions for group by
  
negative.code:                            Vs = [V || {_K,V} <- Ks],
                    V = reverse(maps:get(K, Mapped)),
                    V1 = lists:map(fun(ListVal) ->
                                         xqerl_seq3:from_list(ListVal)
                                   end, V),
                    New = list_to_tuple(Vs ++ V1) ,
                    [New|Acc]
  