     unsigned char *devFiltered;
 
     // Initialize Cuda Memory
-    CHECK(cudaMalloc(&devGrayScale, WIDTH * HEIGHT * sizeof(unsigned char)));
-    CHECK(cudaMalloc(&devFiltered, WIDTH * HEIGHT * sizeof(unsigned char)));
-
+    CHECK(cudaMalloc(&devGrayScale, imageSize));
+    CHECK(cudaMalloc(&devFiltered, imageSize));
 
     // Copy Cuda Memory
-    CHECK(cudaMemcpy(devGrayScale, grayScale.img, WIDTH * HEIGHT * sizeof(unsigned char), cudaMemcpyHostToDevice));
+    CHECK(cudaMemcpy(devGrayScale, grayScale.img, imageSize, cudaMemcpyHostToDevice));
 
     // Set to 0 just in case
-    CHECK(cudaMemset(devFiltered, 0, WIDTH * HEIGHT * sizeof(unsigned char)));
-
-    /*CHECK(cudaBindTexture(NULL, imageTex, devGrayScale, WIDTH * HEIGHT * sizeof(unsigned char)));*/
+    CHECK(cudaMemset(devFiltered, 0, imageSize));
+
     cudaChannelFormatDesc desc = cudaCreateChannelDesc<unsigned char>();
-    CHECK(cudaBindTexture2D(NULL, imageTex, devGrayScale, desc, WIDTH, HEIGHT, sizeof(unsigned char) * WIDTH));
+    CHECK(cudaBindTexture2D(NULL, imageTex, devGrayScale, desc, source.width, source.height, sizeof(unsigned char) * source.width));
 
     // Run the kernel
-    dim3 dimBlock(WIDTH/32, HEIGHT/32);
-    dim3 dimGrid(32, 32);
-    filter<<<dimBlock, dimGrid>>>(devGrayScale, devFiltered);
+    int n = 16;
+    dim3 dimBlock(source.width/n, source.height/n - 20);
+    dim3 dimGrid(n, n);
+    filter<<<dimBlock, dimGrid>>>(devGrayScale, devFiltered, filtered.width, filtered.height);
 
     // Return the Cuda Memory
-    CHECK(cudaMemcpy(filtered.img, devFiltered, WIDTH * HEIGHT * sizeof(unsigned char), cudaMemcpyDeviceToHost));
+    CHECK(cudaMemcpy(filtered.img, devFiltered, imageSize, cudaMemcpyDeviceToHost));
 
     FILE *out;
     if (!(out = fopen(fname2, "wb")))

#include <stdio.h>
#define CHECK(e) { int res = (e); if (res) printf("CUDA ERROR %d\n", res); }
#define THRESH 10000

#define N 480

#define WIDTH 640
#define HEIGHT 480
#define WARP_SIZE 32

texture<unsigned char, 2> imageTex;


    fscanf(src, "%d\n", &ignored);

    int pixels = source.width * source.height;
    int imageSize = source.width * source.height * sizeof(unsigned char);
    source.img = (unsigned char *)malloc(pixels*3);
    if (fread(source.img, sizeof(unsigned char), pixels*3, src) != pixels*3)
    {
