 	dim3 grid ((size - 1) / BLOCK + 1);
 	dim3 block (BLOCK);
 	size_t global_mem_size = size * sizeof(int);
-	size_t transblock_buffers_size = _2XBUFFER_SIZE * (block.x - 1) * sizeof(int);
-	size_t buffer_flags_size = (block.x - 1) * 2 * sizeof(int);
+	size_t transblock_buffers_size = _2XBUFFER_SIZE * (grid.x - 1) * sizeof(int);
+	size_t buffer_flags_size = (grid.x - 1) * 2 * sizeof(int);
 
 	ErrorCheck(cudaMalloc(&global_mem, global_mem_size), "cudaMalloc global");
 	ErrorCheck(cudaMemcpy(global_mem, buffer, global_mem_size, cudaMemcpyHostToDevice),


static __device__ int blocksComplete = 0;

extern "C" __global__ void ParticleSort (unsigned int *global_mem,
					 unsigned int *transblock_buffers,
					 unsigned int *buffer_flags,

	else if ((blockIdx.x < gridDim.x - 1) && (threadIdx.x >= (blockDim.x - BUFFER_SIZE)))
		buff_role = RBUFF;
	else buff_role = NONE;

	volatile unsigned int *const here = beginning + threadIdx.x;

