 
   cudaSobel(d_magnitude,d_direction,d_blur,size);
 
-  gradientMaximumDetector(d_image,d_magnitude,d_direction,size);
-
+  d_edges = gradientMaximumDetector(d_image,d_magnitude,d_direction,size);
+
+  hysteresis(d_edges,size,t1,t2);
+
+  cudaMemcpy(image,d_edges,size.z*sizeof(float),cudaMemcpyDeviceToHost);
+
+  cudaThreadSynchronize();
+  cutStopTimer( timer );  ///< Stop timer
+  printf("cudaCanny total time = %f ms\n",cutGetTimerValue( timer ));
+
+  /// free memory used for image magnitude
   cudaFree(d_direction);
   CUT_CHECK_ERROR("Image direction memory free failed");
   cudaFree(d_magnitude);
   CUT_CHECK_ERROR("Image memory free failed");
-
-  hysteresis(d_image,size,t1,t2);
-
-  cudaMemcpy(image,d_image,size.z*sizeof(float),cudaMemcpyDeviceToHost);
-
-  /// free memory used for image magnitude
   cudaFree(d_image);
   CUT_CHECK_ERROR("Image memory free failed");
 
-  cudaThreadSynchronize();
-  cutStopTimer( timer );  ///< Stop timer
-  printf("cudaCanny total time = %f ms\n",cutGetTimerValue( timer ));
-
-}
+}

  return(d_img);

}

__global__ void kernel_2DSobel(float *Magnitude, short2* Direction, int3 size){
/// This is an less elaborated kernel version that calculate the sobel-x, sobel-y, 

}

extern "C"
float* gradientMaximumDetector(float *d_img, float *d_mag, short2 *d_dir, int3 size){

  int threadsPerBlock = 256;
  int blocksPerGrid = ((size.z) + threadsPerBlock -1) >> 8;
