anchor.diff:  				      VarsToExport2)],
   	    %% EVs are variables from Exprs1 that need to be exported.
   	    NumOfNewVars = num_of_new_vars(SubSubst),
  -	    [{{SubRange2, EVs, SubSubst}, NumOfNewVars}];
  +	    [{{SubRange2, EVs, SubSubst},NumOfNewVars}];
         	false ->
   	    []
       end.
   
   group_clone_pairs(ClonePairs, Thresholds) ->
       ClonePairs1=lists:keysort(2,ClonePairs),
  -    group_clone_pairs(ClonePairs1,Thresholds, 0,[],[]).
  -
  -group_clone_pairs([], _,_, TmpAcc, Acc) ->
  -    case TmpAcc of
  -	[] -> lists:reverse(Acc);
  -	_ -> lists:reverse([TmpAcc|Acc])
  -    end;
  -group_clone_pairs([_ClonePairs={C,NumOfNewVars}|T], Thresholds, Sum, TmpAcc, Acc) ->
  +    group_clone_pairs(ClonePairs1, Thresholds, []).
  +
  +group_clone_pairs([], _, Acc) ->
  +    lists:reverse(Acc);
  +group_clone_pairs(ClonePairs, Thresholds, Acc) ->
  +    MinFreq= Thresholds#threshold.min_freq -1,
  +    {NewCs, LeftPairs}=group_clone_pairs(ClonePairs,Thresholds, sets:new(),[],[]),
  +    NewAcc = case length(NewCs)>=MinFreq of
  +		 true->[NewCs|Acc];
  +		 false ->
  +		     Acc
  +	     end,
  +    case length(LeftPairs)<MinFreq of 
  +	true ->
  +	    NewAcc;
  +	false ->
  +	    group_clone_pairs(LeftPairs, Thresholds, NewAcc)
  +    end.
  +
  +group_clone_pairs([], _, _, Acc, LeftPairs) ->
  +    {lists:reverse(Acc), lists:reverse(LeftPairs)};
  +group_clone_pairs([CP={C={_R, _EVs, Subst}, NumOfNewVars}|T], Thresholds, ExprsToBeGenAcc, Acc, LeftPairs) ->
       MaxNewVars = Thresholds#threshold.max_new_vars,
  -    MinFreq= Thresholds#threshold.min_freq -1,
  -    NewSum = NumOfNewVars+Sum,
  -    case NewSum =< MaxNewVars of 
  -	true ->
  -	    group_clone_pairs(T, Thresholds, NewSum, [C|TmpAcc], Acc);
  +    ExprsToBeGen=exprs_to_be_generalised(Subst),
  +    NewExprsToBeGenAcc =sets:union(ExprsToBeGen, ExprsToBeGenAcc),
  +    case sets:size(NewExprsToBeGenAcc)=<MaxNewVars of
  +    	true ->
  +	    group_clone_pairs(T, Thresholds, NewExprsToBeGenAcc, [C|Acc], LeftPairs);
   	false ->
  -	    case length(TmpAcc)>=MinFreq of 
  +	    case NumOfNewVars>MaxNewVars of 
   		true ->
  -		    case NumOfNewVars=<MaxNewVars of
  -			true ->
  -			    group_clone_pairs(T, Thresholds, NumOfNewVars, [C],
  -					      [lists:reverse(TmpAcc)|Acc]);
  -			false ->
  -			    group_clone_pairs(T, Thresholds, NumOfNewVars, [],
  -					      [lists:reverse(TmpAcc)|Acc])
  -		    end;
  +		    group_clone_pairs([], Thresholds, ExprsToBeGenAcc, Acc, LeftPairs);
   		false ->
  -		    case NumOfNewVars=<MaxNewVars of 
  -			true ->
  -			    group_clone_pairs(T, Thresholds, NumOfNewVars, [C], Acc);
  -			false ->
  -			    group_clone_pairs(T, Thresholds, NumOfNewVars, [], Acc)
  -		    end
  +		    group_clone_pairs(T, Thresholds, ExprsToBeGenAcc, Acc, [CP|LeftPairs])
   	    end
       end.
  -	    
  +
  +%% This is not accurate, and will be improved!
  +exprs_to_be_generalised(SubSt) ->
  +    sets:from_list([refac_prettypr:format(refac_misc:reset_attrs(E1))||
  +		       {E1,_E2}<-SubSt, refac_syntax:type(E1)/=variable]).
  +
  +   
  +num_of_new_vars(SubSt) ->
  +     length(lists:usort([{refac_prettypr:format(refac_misc:reset_attrs(E1)),
  + 			 refac_prettypr:format(refac_misc:reset_attrs(E2))}
  + 			||{E1,E2}<-SubSt, refac_syntax:type(E1)/=variable])).
   
   
       
  
positive.code: -define(ExpHashTab, list_to_atom(filename:join(?WRANGLER_DIR, "plt/exp_hash_tab"))).
  -define(ExpSeqFile, list_to_atom(filename:join(?WRANGLER_DIR, "plt/exp_seq_file"))).
  -define(CloneTab,  list_to_atom(filename:join(?WRANGLER_DIR, "plt/clone_tab"))).
  
  %% record the store the ets/dets table names.
  -record(tabs, 
  	{ast_tab,
  	 var_tab, 
  	 file_hash_tab,
  	 exp_hash_tab,
  	 clone_tab,
  	 tmp_ast_tab
  	}).
  
  %% A record representing a clone class.
  
negative.code:     MaxNewVars = Thresholds#threshold.max_new_vars,
      case Score1 >= SimiScoreThreshold  andalso
  	simi_score(Exprs2, SubExprs2) >= SimiScoreThreshold  andalso
  	sets:size(exprs_to_be_generalised(Subst)) =< MaxNewVars of	
        true ->
  	  [ClonePair];
        false ->
  