   clustering_rebased(std::vector<unsigned int> clustering) {
     std::map<unsigned int, unsigned int> dict;
     // construct dictionary
-    for (unsigned int i=0; i < n_frames; ++i) {
-      if (dict.count(clustering[i]) == 0) {
-        dict[clustering[i]] = i+1;
+    dict[0] = 0;
+    for (unsigned int i=0; i < clustering.size(); ++i) {
+      unsigned int s = clustering[i];
+      if (dict.count(s) == 0) {
+        dict[s] = i+1;
       }
     }
     // rebase

    int n_gpus = get_num_gpus();
    std::vector<float*> d_coords_sorted(n_gpus);
    std::vector<unsigned int*> d_clustering(n_gpus);
    std::vector<unsigned int*> d_ref_states(n_gpus);
    // sort coords (and previous clustering results)
    // according to free energies
    std::vector<float> tmp_coords_sorted(n_rows * n_cols);

    
    
    unsigned int block_rng, i_from, i_to, i, i_gpu;
    // initialize GPUs
    for (i_gpu=0; i_gpu < n_gpus; ++i_gpu) {
      cudaSetDevice(i_gpu);
