     free(dProjection);
     
     if(!fits_in_memory){
-     for (dev = 0; dev < deviceCount; dev++){
-        cudaSetDevice(dev);
-        cudaFree(dProjection_accum[dev]);
-        
-    }
-    free(dProjection_accum);
-    }
-    //cudaFreeArray(d_imagedata);
+        for (dev = 0; dev < deviceCount; dev++){
+            cudaSetDevice(dev);
+            cudaFree(dProjection_accum[dev]);
+            
+        }
+        free(dProjection_accum);
+    }
     cudaCheckErrors("cudaFree d_imagedata fail");
-    
-    
     
     cudaDeviceReset();
     return 0;

        cudaGetDeviceProperties(&deviceProp, dev);
        if (dev>0){
            if (strcmp(devicenames,deviceProp.name)!=0){
                mexWarnMsgIdAndTxt("Ax:GPUselect","Detected one (or more) different GPUs.\n This code is not smart enough to separate the memory GPU wise if they have different computational times or memory limits.\n First GPU parameters used. If the code errors you might need to change the way GPU selection is performed. \n Siddon_projection.cu line 266.");
                break;
            }
        }

    
    // get index of first intersection. eq (26) and (19)
    int i,j,k;
    float aminc=fminf(fminf(ax,ay),az);
    i=(int)floorf(source.x+ (aminc+am)/2*ray.x);
    j=(int)floorf(source.y+ (aminc+am)/2*ray.y);
    k=(int)floorf(source.z+ (aminc+am)/2*ray.z);
    // Initialize
    float ac=am;
    //eq (28), unit anlges
    float axu,ayu,azu;
    axu=1/fabs(ray.x);
    ayu=1/fabs(ray.y);
    azu=1/fabs(ray.z);
    // eq(29), direction of update
    float iu,ju,ku;
    iu=(source.x< pixel1D.x)? 1 : -1;
    ju=(source.y< pixel1D.y)? 1 : -1;
    ku=(source.z< pixel1D.z)? 1 : -1;
    
    float maxlength=sqrtf(ray.x*ray.x*geo.dVoxelX*geo.dVoxelX+ray.y*ray.y*geo.dVoxelY*geo.dVoxelY+ray.z*ray.z*geo.dVoxelZ*geo.dVoxelZ);
    float sum=0;
    unsigned int Np=(imax-imin+1)+(jmax-jmin+1)+(kmax-kmin+1); // Number of intersections
    // Go iterating over the line, intersection by intersection. If double point, no worries, 0 will be computed
