anchor.diff:        #rete{bnodes=[],anodes=[]}=Tree) ->
       merge(Path,Tree#rete{bnodes=[make_terminal_bnode()]});
   merge(#rete{bnodes=BNodesLeft,anodes=ANodesLeft,pnodes=PNodesLeft},
  -      #rete{bnodes=[Root|_]=BNodesRight,anodes=ANodesRight,pnodes=PNodesRight}=Tree) ->
  +      #rete{bnodes=[Root|_]=BNodesRight,anodes=ANodesRight,pnodes=PNodesRight,bnodes_map=BNodeChangeMap}=Tree) ->
       %% Merge beta layers
       GetChildrenFun=
           fun(#bnode{bnode_ids=ChildrenIds})->
  
positive.code:         % In this case we just mark that tree node have an output (pnodes)
          {[#bnode{id=Id,pnodes=PIds}=H|_], []} ->
              #bnode{id=IdL,pnodes=PIdsL}=lists:last(BNodesLeft),
              % Update pnodes bnode_ids: replace an old bnode id (IdL) with a new one (Id)
              PNodesLeft1=[ P#pnode{bnode_ids=lists_replace(IdL, Id, BIds)} || #pnode{bnode_ids=BIds}=P <-PNodesLeft],
              BNodesRight1=lists:keyreplace(Id, #bnode.id, BNodesRight, H#bnode{pnodes=umerge_lists(PIds,PIdsL)}),
  
negative.code:                    Acc#anode{bnode_ids=umerge_lists(Bids, AccBids)}
             end,HAN,Tail) || [HAN|Tail]<-EquimatchComponents],
      % 4. Return resulting rete
  %%     DBG=[{PL,PR}||#pnode{id=X}=PL<-PNodesLeft1,#pnode{id=X1}=PR<-PNodesRight,X==X1,PL=/=PR],
  %%     io:format("~p~n",[{'CLASH!',DBG}]),
      BNodesRight2=apply_bnode_map(BNodeChangeMap1,BNodesRight1),
      ANodesRight3=apply_bnode_map(BNodeChangeMap1,ANodesRight2),
      PNodes=apply_bnode_map(BNodeChangeMap1,ukeymerge_lists(#pnode.id,PNodesLeft1,PNodesRight)),
      Tree#rete{bnodes=BNodesRight2,anodes=ANodesRight3,pnodes=PNodes,bnodes_map=BNodeChangeMap1}.
  
  apply_bnode_map(_BNodeChangeMap,[])->
      [];
  apply_bnode_map([],Nodes)->
      Nodes;
  apply_bnode_map(BNodeChangeMap,Nodes)->
      Map=fun(Ids) ->
                  [follow_map(Id, BNodeChangeMap)||Id <- Ids]
          end,
      [case Node of
           #pnode{bnode_ids=Ids} -> Node#pnode{bnode_ids=Map(Ids)};
           #anode{bnode_ids=Ids} -> Node#anode{bnode_ids=Map(Ids)};
           #bnode{bnode_ids=Ids} -> Node#bnode{bnode_ids=Map(Ids)}
       end || Node <- Nodes].
  
  follow_map(Id, BNodeChangeMap) ->
      case lists:keyfind(Id, 1, BNodeChangeMap) of
          false -> Id;
          {Id, NextId} -> follow_map(NextId, BNodeChangeMap)
      end.
  
  lists_replace(OldElem, NewElem, List) ->
      [if OldElem == Elem ->
  