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:             ANodesRight1=ANodesRight;
          % In this case we tailoring a new branch by fixing ids of a tree node
          {[#bnode{id=Id1,bnode_ids=Ids}=H|_], [#bnode{id=Id}|_]=LeftRest} ->
  %%             io:format("~p~n",[{'BNODE-BRANCH',Id1}]),
              PNodesLeft1=PNodesLeft,
              BNodeChangeMap1=BNodeChangeMap,
              BNodesRight1=lists:keyreplace(Id1, #bnode.id, BNodesRight, H#bnode{bnode_ids=[Id|Ids]})++LeftRest,
              %% Filter out alpha nodes that point to bnodes trunceted after merge of beta layer
              % Dou to alpha node may have multiple pointers to bnodes we firstly remove that 
  