anchor.diff:  %% CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 
   %% SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR 
   %% BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 
  -%% WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 
  +%% WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLI
   %% OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 
   %% ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
   %% =====================================================================
  
positive.code: 		 [?MODULE, OutFile, NotCareMods, SearchPaths, WithLabel]),
      NotCareMods1 = [list_to_atom(T)|| T<-string:tokens(NotCareMods, ", ")],
      refac_module_graph:module_graph_to_dot(OutFile, NotCareMods1, SearchPaths, WithLabel).
     
  -spec(gen_module_scc_graph/3::(filename(), [filename()|dir()], boolean()) ->true).
  gen_module_scc_graph(OutFile, SearchPaths, WithLabel)->
      ?wrangler_io("\nCMD: ~p:gen_module_scc_graph(~p, ~p, ~p).\n",
  		 [?MODULE, OutFile, SearchPaths, WithLabel]),
      refac_module_graph:scc_graph_to_dot(OutFile, SearchPaths, WithLabel).
      
  -spec(gen_function_callgraph/3::(filename(), filename(),[filename()|dir()]) ->true).
  gen_function_callgraph(OutFile, FileName, SearchPaths)->
       ?wrangler_io("\nCMD: ~p:gen_function_callgraph(~p, ~p, ~p).\n",
   		 [?MODULE, OutFile, FileName, SearchPaths]),
       wrangler_callgraph_server:fun_callgraph_to_dot(OutFile, FileName).
     
  
  %% Detetect those module dependencies where the dependeny is only caused by those non-API functions.
  %% Use ibrowse (part of couchdb) as an example.
  
  %% Check cases studies: counch db; wrangler; refactorerl; and ibrowse.
  
  
  %% Question: structure it is possible to find a target module, but how about semantically?.
  
  %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  %%                                                                     %%
  %%  Detecte and Remove Improper Inter-module calles.                   %%
  %%                                                                     %%
  %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  
  improper_inter_module_calls(OutFile, SearchPaths, NotCareMods) ->
      ModCallerCallees = refac_module_graph:module_graph_with_funs(SearchPaths),
      FullMG = digraph:new(), 
      refac_module_graph:add_edges(ModCallerCallees,[],FullMG),
      Files = refac_util:expand_files(SearchPaths, ".erl"),
      FunCallees = lists:flatmap(fun non_api_funs/1, Files),
      MG = lists:append([[{CallerMod, [{CalleeMod, CalledFuns}]}|| 
  			   {CalleeMod, CalledFuns} <- CalleeMods,
  			   not lists:member(CalleeMod, NotCareMods),
  			   is_sub_list([{CalleeMod, F, A} || {F, A} <- CalledFuns],FunCallees)]
  		       || {CallerMod, CalleeMods} <- ModCallerCallees,
  			  not lists:member(CallerMod, NotCareMods)]),
      
      Edges = [{CallerMod, CalleeMod, CalleeFun} || 
  		{{CallerMod,_}, CalleeMods}<-MG,
  		{CalleeMod, CalleeFuns}<-CalleeMods, 
  		CalleeFun<-CalleeFuns],
      GroupedEdges=refac_misc:group_by(3, Edges),
      Res =[find_best_target_module(EdgeGroup, FullMG)||EdgeGroup<-GroupedEdges],
      refac_module_graph:module_graph_to_dot(OutFile, MG, true),
      digraph:delete(FullMG),
      Res.
  
  find_best_target_module(EdgeGroup, MG) ->
      {CalledMods, DefMod1, Fun1}=lists:unzip3(EdgeGroup),
      find_best_target_module_1(hd(Fun1), hd(DefMod1), [hd(DefMod1)|CalledMods], MG).
  
  find_best_target_module_1({F, A}, DefMod, UsedMods, MG) ->
      RNB =lists:append([digraph:out_neighbours(MG, Mod)--[DefMod]||Mod<-UsedMods]),
      RNB1 =sort_list_by_freq(RNB),
      case RNB1 of 
  	[] ->
  	    {{DefMod, F, A}, target_module_not_found};
  	_ ->
  	   {{DefMod, F, A}, element(1, hd(RNB1))}
      end.
  
  	
  sort_list_by_freq(L) ->
      L1 = sort_list_by_freq(L, []),
      lists:reverse(lists:keysort(2, L1)).
  		 
  sort_list_by_freq([], Acc) ->
      Acc;
  sort_list_by_freq([H|L], Acc) ->
      case lists:keysearch(H,1, Acc) of
  	{value, {H, F}} ->
  	    sort_list_by_freq(L, lists:keyreplace(H, 1, Acc, {H, F+1}));
  	false ->
  	    sort_list_by_freq(L, lists:keystore(H,1, Acc, {H,1}))
      end.
  %% Not very interesting. (too many false positives).
  improper_located_apis(OutFile, SearchPaths, NotCareMods) ->
      ModCallerCallees = refac_module_graph:module_graph_with_funs(SearchPaths),
      Files = refac_util:expand_files(SearchPaths, ".erl"),
      APIFuns = lists:flatmap(fun api_funs/1, Files),
      LabelFuns = lists:append([[{CalleeMod, F, A} || {F, A} <- CalledFuns]
  			      || {_CallerMod, CalleeMods} <- ModCallerCallees,
  				 {CalleeMod, CalledFuns} <- CalleeMods]),
      FunsUsedByOnlyOneMod = get_uniques(LabelFuns),
      APIFunsUsedByOnlyOneMod = list_insection(APIFuns, FunsUsedByOnlyOneMod),
      Res = [[{CallerMod, [{CalleeMod, [{F, A} || 
  					 {F, A} <- CalledFuns, 
  					 lists:member({CalleeMod, F, A}, APIFunsUsedByOnlyOneMod)]}]}
  	    || {CalleeMod, CalledFuns} <- CalleeMods]
  	   || {CallerMod, CalleeMods} <- ModCallerCallees],
      MG = lists:append(Res),
      MG1 = [{{CallerMod, CallerFile}, [{CalleeMod, CalledFuns} || {CalleeMod, CalledFuns} <- CalleeMods, CalledFuns /= [],
  								 not lists:member(CalleeMod, NotCareMods)]}
  	   || {{CallerMod, CallerFile}, CalleeMods} <- MG, not lists:member(CallerMod, NotCareMods)],
      refac_module_graph:module_graph_to_dot(OutFile, MG1, true).
  
  
  get_uniques(L) ->    
      get_uniques(L, [],[]).
  
  get_uniques([], Acc, _) ->
       Acc;
  get_uniques([H|L], Acc, Acc1) ->
      case lists:member(H, Acc) of 
  	true ->
  	    get_uniques(L, Acc--[H], [H|Acc1]);
  	false ->
  	    case lists:member(H, Acc1) of 
  		true ->
  		    get_uniques(L, Acc, Acc1);
  		false ->
  		    get_uniques(L, [H|Acc], Acc1)
  	    end
      end.
  
  %%TODO: compare the result of using NonAPIs and ClosedNonAPIs.
  non_api_funs(File) -> 
      element(3, api_and_non_api_funs(File)).
  
  
  api_funs(File) ->
      element(1, api_and_non_api_funs(File)).
      
      
  api_and_non_api_funs(File) ->
      ExpFuns = exported_funs(File),
      #callgraph{callercallee = CallerCallees} =
  	wrangler_callgraph_server:get_callgraph([File]),
      CG = digraph:new(),
      add_edges(CallerCallees, CG),
      Vs = digraph:vertices(CG),
      SCCs = [SCC || SCC <- digraph_utils:strong_components(CG), length(SCC) > 1],
      Reaching = [{V, digraph_utils:reaching([V], CG)}
  		|| V <- Vs, lists:member(V, ExpFuns)],
      APIs = [V || {V, Rs} <- Reaching, lists:member(V, ExpFuns), Rs == [V] orelse
  								  lists:any(fun (SCC) -> Rs == SCC
  									    end, SCCs)],
      SemiAPIs = [V || V <- ExpFuns -- APIs, similar_to_some_api(V, APIs, CG)],
      NonAPIs = ExpFuns -- APIs ++ SemiAPIs,
      ClosedNonAPIs = [V || V <- NonAPIs, is_closed(V, CG)],
      digraph:delete(CG),
      {APIs, SemiAPIs, NonAPIs, ClosedNonAPIs}.
  
  
  is_closed(V= {M, _F, _A}, CG) ->
      Rs = [{M1, F1, A1} || {M1, F1, A1} <- digraph_utils:reachable([V], CG),
  			  M == M1],
  
negative.code: cyclic_dependent_modules(OutFile, SearchPaths, WithLabel) ->
      ?wrangler_io("\nCMD: ~p:cyclic_dependent_modules(~p, ~p, ~p).\n",
  		 [?MODULE, OutFile, SearchPaths, WithLabel]),
      Files = refac_util:expand_files(SearchPaths, ".erl"),
      ModCallerCallees = refac_module_graph:module_graph_with_funs(SearchPaths),
      MG = digraph:new(),
      refac_module_graph:add_edges(ModCallerCallees,[],MG),
      SCCs = digraph_utils:cyclic_strong_components(MG),
      Cycles = [digraph:get_short_cycle(MG, V) || SCC<-SCCs, V <-SCC],
      Cycles1 = remove_duplicated_cycles(Cycles),
      CycleWeakestLinkPairs= [{C, break_a_cycle(C, ModCallerCallees, MG, Files)}|| C<-Cycles1],
      cycles_to_dot(MG, OutFile,CycleWeakestLinkPairs),
      digraph:delete(MG).
  
  
  remove_duplicated_cycles(Cs) ->
      remove_duplicated_cycles(Cs, []).
  
  remove_duplicated_cycles([], Acc) ->
      Acc;
  remove_duplicated_cycles([C|Cs], Acc) ->
      case lists:any(fun(C1) ->
  			   lists:usort(C)==lists:usort(C1) 
  		   end, Acc) of 
  	true ->
  	    remove_duplicated_cycles(Cs, Acc);
  	false ->
  	    remove_duplicated_cycles(Cs, [C|Acc])
      end.
  	
  
  cycles_to_dot(MG, OutFile, CycleWeakestLinkPairs)->
      CycleEdges=[get_cycle_edges(MG, C, WeakestLink)||
  		   {C, WeakestLink}<-CycleWeakestLinkPairs],
      NewEdgeList=get_renamed_edge_list(CycleEdges),
      edge_list_to_dot(lists:append(NewEdgeList), OutFile, "ModuleGraph", true).
     
      
  get_cycle_edges(MG, C, WeakestLink) ->
      get_cycle_edges(MG, C, WeakestLink, []).
  
  get_cycle_edges(MG, T, {WeakestLink, NodeToColor}, Edges)  when length(T)=<1->
      Edges1 =lists:reverse(Edges),
      [{X, Y, Label, {X,Y}==WeakestLink, case X==NodeToColor of
  					   true -> 1;
  					   false -> 0
  				       end}|| E<-digraph:edges(MG),
  				       {_, X, Y, Label} <- [digraph:edge(MG, E)],
  				       lists:member({X, Y}, Edges1)];
  get_cycle_edges(MG,[V1, V2|T], WeakestLink,  Edges) ->
  