public void mergeSets(){

        //System.out.println("");

        // We repeat this process until we have at least 3 clusters
        while (spagenumbers.size()>3){

            ArrayList<RGB> averages = new ArrayList<RGB>();

            // Obtaining the average color for each cluster
            for (int i = 0; i<this.spagenumbers.size(); i++){
                ArrayList<Integer> pages = this.spagenumbers.get(i);
                RGB ave = this.averageColorInSet(pages, this.rgbs);
                averages.add(ave);
            }

            //System.out.println(averages);

            //System.out.println("");

            ArrayList<Double> distances = new ArrayList<Double>();

            // Obtaining the distances between each set of pages
            for (int i = 0; i<averages.size()-1; i++){
                double distance = distanceRGB(averages.get(i),averages.get(i+1));
                distances.add(distance);
            }

            //System.out.println("Distances: "+distances);

            // Search for the minimum distance between a pair of sets
            Double min = Double.MAX_VALUE;
            for (int i = 0; i<distances.size(); i++){
                if (distances.get(i)<min)
                    min = distances.get(i);
            }
            int index = distances.indexOf(min);

            // Now that we know what sets we have to join, lets just join them
            ArrayList<Integer> newset = new ArrayList<Integer>();
            for (int i=0; i<spagenumbers.get(index).size(); i++)
                newset.add(spagenumbers.get(index).get(i));
            for (int i=0; i<spagenumbers.get(index+1).size(); i++)
                newset.add(spagenumbers.get(index+1).get(i));

            spagenumbers.remove(index+1);
            spagenumbers.set(index, newset);

            //System.out.println(spagenumbers);
            
        }

        //System.out.println("");

    }