Ported knn-merging code to work with an arbitrary number of clusters