   cudaMalloc( (void**)&finished_device, sizeof(int));
   cudaMemcpy( data_device, data, DATA_DIM*DATA_DIM*DATA_DIM*sizeof(unsigned char), cudaMemcpyHostToDevice);
   cudaMemcpy( region_device, region, DATA_DIM*DATA_DIM*DATA_DIM*sizeof(unsigned char), cudaMemcpyHostToDevice);
-  cudaMemcpy( finished_device, finished, sizeof(int), cudaMemcpyHostToDevice);
+  //cudaMemcpy( finished_device, finished, sizeof(int), cudaMemcpyHostToDevice);
 
   dim3 dimBlock( 8, 8, 8 );
   dim3 dimGrid( 64, 64, 64 );
 
-  while(finished > 0){
-
+  while(finished[0] == 0){
+    finished[0] = 1;
+    cudaMemcpy( finished_device, finished, sizeof(int), cudaMemcpyHostToDevice);
     region_grow_kernel<<<dimGrid, dimBlock>>>(data_device, region_device, finished_device);
     cudaMemcpy( finished, finished_device, sizeof(int), cudaMemcpyDeviceToHost);
-
-  }
-
+    printf("Finished: %i\n", finished[0]);
+
+  }
+  cudaMemcpy( region, region_device, DATA_DIM*DATA_DIM*DATA_DIM*sizeof(unsigned char), cudaMemcpyDeviceToHost);
   return region;
   }
 



// Check if two values are similar, threshold can be changed.
int similar(unsigned char* data, int3 a, int3 b){
  unsigned char va = data[a.z * DATA_DIM*DATA_DIM + a.y*DATA_DIM + a.x];
  unsigned char vb = data[b.z * DATA_DIM*DATA_DIM + b.y*DATA_DIM + b.x];



  //printf("Data created");

  unsigned char* region = grow_region_gpu(data);

  //printf("Region grown");

