anchor.diff:  	    ok;
   	_Msg -> 
   	    ?wrangler_io("Unexpected message:\n~p\n",[_Msg]),
  -	     ok	
  +	    clone_check_loop(Cs)
       end.
    
   %%=============================================================================
  -examine_clone_sets([], _MinFreq, _SimiScore, ASTTab, _VarTab, Pid, _Num)->
  +examine_clone_class_sets([], _MinLen, _MinFreq, _SimiScore, ASTTab, _VarTab, Pid, _Num) ->
       Pid ! {get_clones, self()},
       receive
  -	{Pid, Cs} ->
  -	    [get_generalised_form(ASTTab,C)||C<-Cs]
  +      {Pid, Cs} ->
  +	  [get_generalised_form(ASTTab, C) || C <- Cs]
       end;
  -examine_clone_sets([C|Cs],MinFreq, SimiScore, ASTTab, VarTab, Pid, Num) ->
  +examine_clone_class_sets([C| Cs], MinLen, MinFreq, SimiScore, ASTTab, VarTab, Pid, Num) ->
       ?wrangler_io("\nChecking the ~pth clone candidate...", [Num]),
  -    Res =examine_a_clone_set(C, MinFreq, SimiScore, ASTTab, VarTab,Pid),
  +    {Res,_}= examine_a_clone_class_set(C, MinLen, MinFreq, SimiScore, ASTTab, VarTab, Pid),
       add_new_clones(Pid, Res),
  -    examine_clone_sets(Cs, MinFreq, SimiScore, ASTTab, VarTab,Pid, Num+1).
  -
  -examine_a_clone_set(Cs, MinFreq, SimiScore, ASTTab, VarTab, Pid) ->
  -    case Cs of 
  -	[] -> []; %% why should this happen?
  +    examine_clone_class_sets(Cs, MinLen, MinFreq, SimiScore, ASTTab, VarTab, Pid, Num + 1).
  +
  +examine_a_clone_class_set(C, MinLen, MinFreq, SimiScore, ASTTab, VarTab, Pid) ->
  +    refac_io:format("C:\n~p\n", [C]),
  +    {Ranges, {Len, Freq}}=C,
  +    Res = examine_a_clone_class(C, MinFreq, SimiScore, ASTTab, VarTab),
  +    case Res of
  +	[R] when element(2, R) == Freq ->
  +	    %% the clone class candiate is a real clone clones;
  +	    %% the checking of this clone set finishes here;
  +	    {Res,[]};
   	_ ->
  -	    C={_, {_, Freq}} = lists:last(Cs), 
  -	    refac_io:format("C:\n~p\n", [C]),
  -	    Res =examine_a_clone_class(C, MinFreq, SimiScore, ASTTab, VarTab),
  -	    case Res of
  -		[R]  when element(2, R)==Freq ->
  -		    Res;
  -		_ -> Cs1 = lists:sublist(Cs, 1, length(Cs)-1),
  -		     examine_a_clone_set(group_cs(Cs1,[]), MinFreq, SimiScore, ASTTab, VarTab,Pid, Res)
  +	    case Len-MinLen>1 of 
  +		true ->
  +		    %% the clone class candidate is devided into smaller ones;
  +		    %% continue checking the remainng ones;
  +		    NewRanges0=sub_list(Ranges, Len-1),
  +		    NewRanges=zip(NewRanges0),
  +		    NewCs =[{Rs, {length(hd(Rs)), Freq}} || Rs <- NewRanges],
  +		    examine_clone_class_sets_1(NewCs, MinLen, MinFreq, SimiScore, ASTTab, VarTab, Pid,Res);  
  +		false ->
  +		    {Res,[]}
   	    end
       end.
  -
  -examine_a_clone_set([], _MinFreq, _SimiScore, _ASTTab, _VarTab, _Pid, Acc) ->
  -    remove_sub_clones(lists:keysort(2,Acc));  
  -examine_a_clone_set([Cs1|Cs2], MinFreq, SimiScore, ASTTab, VarTab,Pid, Acc) ->
  -    Res = lists:append([examine_a_clone_class(C, MinFreq, SimiScore, ASTTab, VarTab) || C <-Cs1]),
  -    case Res of
  -	[] -> remove_sub_clones(lists:keysort(2,Acc));    
  -    	_ ->examine_a_clone_set(Cs2, MinFreq, SimiScore, ASTTab, VarTab, Pid, Res++Acc)
  -    end.
  -  
  -
  -group_cs([], Acc) ->
  -     lists:reverse(Acc);
  -group_cs([{_Ranges, {Len, _Freq}}], Acc) ->
  -    group_cs([],[[{_Ranges, {Len, _Freq}}]|Acc]);
  -group_cs(Cs=[{_Ranges, {Len, _Freq}}|_T], Acc) ->
  -    {Cs1, Cs2} =lists:splitwith(fun(C)-> element(1,element(2, C))==Len end, Cs),
  -    group_cs(Cs2, [Cs1|Acc]).
  -
  -
  -remove_fun_info(Cs) ->
  -    [remove_fun_info_1(C) || C <-Cs].
  -remove_fun_info_1({Ranges, {Len, Freq}, C}) ->
  -    Ranges1=[{{F, SLine, SCol}, {F, EndLine, EndCol}} || 
  -		{{F,_, _},{SLine, SCol}, {EndLine, EndCol}} <-Ranges],
  -    {Ranges1, Len, Freq, C}.
  + 
  +
  +examine_clone_class_sets_1([], _, _, _, _, _, _, Acc) -> {Acc,[]};
  +examine_clone_class_sets_1([C| Cs], MinLen, MinFreq, SimiScore, ASTTab, VarTab, Pid, Acc) ->
  +    {Res, NewCs}= examine_a_clone_class_set(C, MinLen, MinFreq, SimiScore, ASTTab, VarTab, Pid),
  +    examine_clone_class_sets_1(NewCs++Cs, MinLen, MinFreq, SimiScore, ASTTab, VarTab, Pid, Acc++Res).
   
   
   remove_sub_clones(Cs) ->
  
positive.code:     VarTab = ets:new(var_tab, [set, public]),
      ASTPid = start_ast_process(ASTTab),
      case Files of
  	[] ->
  	     ?wrangler_io("Warning: No files found in the searchpaths specified.", []);
  	_ ->
  	    _Time1 = now(),
  	    generalise_and_hash_ast(Files, ASTPid, MinLen, VarTab,SearchPaths, TabWidth),
  	    Dir = filename:dirname(hd(Files)),
  	    Cs=get_clones(ASTPid, MinLen,MinToks,  MinFreq, Dir),
  	    refac_io:format("Cs:\n~p\n", [Cs]),
  	    ?debug("\nInitial candiates finished\n",[]),
       	    ?wrangler_io("\nNumber of initial clone candidates: ~p\n", [length(Cs)]),
       	    CloneCheckerPid = start_clone_check_process(),
       	    Cs2 = examine_clone_sets(Cs, MinFreq,SimiScore, ASTTab, VarTab, CloneCheckerPid, 1),
  	    refac_io:format("Cs2:\n~p\n", [Cs2]),
  	    stop_clone_check_process(CloneCheckerPid),
     	    _Time2=now(),
  	    ?wrangler_io("generalise and hash ast finished.\n",[])
      end,
      stop_ast_process(ASTPid).
     
  
  
   %% ?debug("current time:~p\n", [time()]),
   %% ?debug("total time used:~p\n", [timer:now_diff(_Time2,_Time1)/1000000]),	    
  	
  generalise_and_hash_ast(Files, ASTPid, MinLen, VarTab, SearchPaths, TabWidth) ->	     
      lists:foreach(fun(File) ->
  			  generalise_and_hash_ast_1(File, ASTPid, MinLen, VarTab, SearchPaths, TabWidth)
  		  end, Files).
  
  
  
  generalise_and_hash_ast_1(FName, ASTPid, MinLen, VarTab, SearchPaths, TabWidth) ->
  
negative.code: 
  ast_loop(ASTTab, {CurM, CurF, CurA, Index, HashPid}) ->
      receive
        {add, {{M, F, A}, ExprASTs}} ->
  	  case {M, F, A} == {CurM, CurF, CurA} of
  	    true ->
  		Len = length(ExprASTs),
  		ExprASTsWithIndex = lists:zip(ExprASTs, lists:seq(0, Len - 1)),
  		[begin
  		   ets:insert(ASTTab, {{M, F, A, Index + I}, E}),
  		   NoOfToks = no_of_tokens(E),
  		   E1 = do_generalise(E),
  		   HashVal = erlang:md5(refac_prettypr:format(E1)),
  		   insert_hash(HashPid, {HashVal, {{M, F, A, Index + I}, NoOfToks}})
  		 end || {E, I} <- ExprASTsWithIndex],
  		insert_dummy_entry(HashPid),
  		ast_loop(ASTTab, {CurM, CurF, CurA, Index + Len, HashPid});
  	    false ->
  		  Len = length(ExprASTs),
  		  ExprASTsWithIndex = lists:zip(ExprASTs, lists:seq(1, Len)),
  		  [begin
  		       ets:insert(ASTTab, {{M, F, A, I}, E}),
  		       NoOfToks = no_of_tokens(E),
  		       E1 = do_generalise(E),
  		       HashVal = erlang:md5(refac_prettypr:format(E1)),
  		       insert_hash(HashPid, {HashVal, {{M, F, A, I}, NoOfToks}})
  		   end || {E, I} <- ExprASTsWithIndex],
  		  insert_dummy_entry(HashPid),
  		  ast_loop(ASTTab, {M, F, A, Len + 1, HashPid})
  	  end;
        {get_clones, From, MinLen, MinToks, MinFreq, Dir} ->
  	  Cs = get_clones(HashPid, MinLen, MinToks, MinFreq, Dir),
  	  From ! {self(), Cs},
  	  ast_loop(ASTTab, {CurM, CurF, CurA, Index, HashPid});
        stop ->
  	  stop_hash_process(HashPid),
  	  ets:delete(ASTTab),
  	  ok
      end.
  	    
      
  do_generalise(Node) ->
      F0 =fun (T, _Others) ->
  		case variable_replaceable(T) of
  		    true ->
  			{refac_syntax:variable('Var'), true};
  		    false -> {T, false}
  