 		M1[tid * 3 + 1] = green;
 		M1[tid * 3 + 2] = red;
 
+		M2[tid * 3]     = 0;
+		M2[tid * 3 + 1] = 0;
+		M2[tid * 3 + 2] = 0;
+
 		// Initialize pixel count
-		n[tid * 3]     = 1;
-		n[tid * 3 + 1] = 1;
-		n[tid * 3 + 2] = 1;
+		n[tid] = 1;
 	}
 	else
 	{
-		// This thread has nothing to contribute
+		// This thread is outside the bounds of the
+		// image and therefore has nothing to contribute
 		// to the final result
-		n[tid * 3]     = 0;
-		n[tid * 3 + 1] = 0;
-		n[tid * 3 + 2] = 0;
-	}
-
-	M2[tid * 3]     = 0;
-	M2[tid * 3 + 1] = 0;
-	M2[tid * 3 + 2] = 0;
+		n[tid] = 0;
+	}
 	
     __syncthreads();
 

	// Thread index within block - used for addressing smem below
	const unsigned int tid = threadIdx.y * blockDim.x + threadIdx.x;

	// Shared memory per channel per thread = 1 long, 2 floats.
	// So a 3 channel image needs 3 longs and 6 floats
	// Thread blocks are up to 24x24 images, one thread per pixel
	// TODO : fixme for variable sized thread blocks
	__shared__ float M1[32*32*3];
	__shared__ float M2[32*32*3];
	__shared__ unsigned int n[32*32*3];

	// 2D Index of current thread
	const int xIndex = blockIdx.x * blockDim.x + threadIdx.x;

	{
		// M1 is the mean already - nothing extra needed
		// calculate stddev from M2 and n
		M2[tid] = sqrt(M2[tid] / n[0]);
	}
	__syncthreads();

