anchor.diff:                   end,
      NewCtx1 = set_variable_tuple_name(NewCtx, NextTupleVar),
      flwor(NewCtx1, T, RetId, Return, NewInternal, 
  -         FunAbs ++ Global,NextTupleVar,false);
  +         FunAbs ++ Global,NextTupleVar,false,Rems);
   
   % where
   flwor(Ctx, [{where,Id,_} = W|T], RetId, Return, Internal, 
  -      Global,TupleVar,_Inline) ->
  +      Global,TupleVar,_Inline,[Rem|Rems]) ->
      NextTupleVar = {var,?L,next_var_tuple_name()},
      CurrContext = {var,?L,get_context_variable_name(Ctx)},
      ThisFun = glob_fun_name({where,Id}),
  -   {NewCtx,FunAbs} = where_part(Ctx, W, []),
  +   {NewCtx,FunAbs} = where_part(Ctx, W, [], Rem),
      
      Call2 = [?P("_@NextTupleVar = '@ThisFun@'(_@CurrContext,xqerl_seq3:flatten(_@TupleVar))")],
      NewInternal = Internal ++ Call2,
      NewCtx1 = set_variable_tuple_name(NewCtx, NextTupleVar),
      flwor(NewCtx1, T, RetId, Return, NewInternal, 
  -         FunAbs ++ Global,NextTupleVar,false);
  +         FunAbs ++ Global,NextTupleVar,false,Rems);
   
   % count
   flwor(Ctx, [{count,_} = C|T], RetId, Return, Internal, 
  -      Global,TupleVar,_Inline) ->
  +      Global,TupleVar,_Inline,[_Rem|Rems]) ->
      NextTupleVar = {var,?L,next_var_tuple_name()},
      CurrContext = {var,?L,get_context_variable_name(Ctx)},
      ThisFun = glob_fun_name(C),
  
positive.code:    {NewCtx,[R1,R2]}.
  
  group_part(#{grp_variables := GrpVars,
               variables     := Vars} = Ctx, {group_by,Id,Clauses}) ->
     % 1. seperate the local/group-able variables from the out-of-scope variables.
     AllInScopeVars = [N || {_,_,_,N} <- Vars, is_atom(N)],
     GroupVars      = [N || {_,_,_,N} <- GrpVars],
  
negative.code:    NewInternal = Internal ++ Call2,
     NewCtx1 = set_variable_tuple_name(NewCtx, NextTupleVar),
     flwor(NewCtx1, T, RetId, Return, NewInternal, 
           FunAbs ++ Global,NextTupleVar,false,[Rem ++ Rem2|Rems]).
  
  scan_variable_usage(Loop, Return) -> % 2170
     FMap = fun({'for', #xqVar{id = Id, position = #xqPosVar{id = PId}}, _}) ->
                  {true, [local_variable_name(Id), local_variable_name(PId)]};
               ({'for', #xqVar{id = Id}, _}) ->
                  {true, [local_variable_name(Id)]};
               ({'let', #xqVar{id = Id}, _}) ->
                  {true, [local_variable_name(Id)]};
               ({'count', #xqVar{id = Id}, _}) ->
                  {true, [local_variable_name(Id)]};
               (#xqWindow{win_variable = #xqVar{id = Id}}) ->
                  {true, [local_variable_name(Id)]};
               (_) ->
                  false
            end,
     Local = lists:append(lists:filtermap(FMap, Loop)),
     
     UMap = fun(Exp) ->
                  A = collect_variable_refs(Exp),
                  U = lists:usort(A),
                  [I || I <- U, lists:member(I, Local)]
            end,
     Used = lists:map(UMap, Loop ++ [Return]),
     
     RFold = fun(U, Acc) ->
                   {[I || I <- U, not lists:member(I, Acc)], U ++ Acc}
             end,
     {Last,  _} = lists:mapfoldr(RFold, [], Used),
     %?dbg("Last", Last),
     Last.
  
  
  collect_variable_refs(Expr) ->
     collect_variable_refs(Expr, []).
  
  collect_variable_refs({variable, Var}, Acc) ->
     [Var|Acc];
  collect_variable_refs(Tup, Acc) when is_tuple(Tup) ->
     collect_variable_refs(erlang:tuple_to_list(Tup), Acc);
  collect_variable_refs(List, Acc) when is_list(List) ->
     lists:flatten([collect_variable_refs(L) || L <- List]) ++ Acc;
  collect_variable_refs(_, Acc) ->
     Acc.
  
  return_part(Ctx,{Id, Expr},IsList) ->
     FunctionName = glob_fun_name({return,Id}),
  