 		unsigned labels_index = row * (labels.step / labels.elem_size) + col;
 
 		if (row < labels.rows && col < labels.cols) {
-			labels[labels_index] = Find(labels.data, labels_index);
+			unsigned label = labels.data[labels_index];
+			if (label < labels_index) {
+				labels[labels_index] = Find(labels.data, label);
+			}
 		}
 	}
 

		grid_size_ = dim3((((d_img_.cols + 1) / 2) + BLOCK_COLS - 1) / BLOCK_COLS, (((d_img_.rows + 1) / 2) + BLOCK_ROWS - 1) / BLOCK_ROWS, 1);
		block_size_ = dim3(BLOCK_COLS, BLOCK_ROWS, 1);

		InitLabeling << <grid_size_, block_size_ >> >(d_img_labels_);

		//cuda::GpuMat d_expanded_connections;
		//d_expanded_connections.create(d_connections_.rows * 3, d_connections_.cols * 3, CV_8UC1);


		if (row < labels.rows && col < labels.cols) {

			unsigned P = 0;

			char buffer[4];

			if (col + 1 < img.cols) {
				// This does not depend on endianness
				*(reinterpret_cast<int16_t*>(buffer)) = *(reinterpret_cast<int16_t*>(img.data + img_index));

				if (row + 1 < img.rows) {
					*(reinterpret_cast<int16_t*>(buffer + 2)) = *(reinterpret_cast<int16_t*>(img.data + img_index + img.step));
				}
			}
			else {
				buffer[0] = img.data[img_index];
				buffer[1] = 0;

				if (row + 1 < img.rows) {
					buffer[2] = img.data[img_index + img.step];
				}
			}

			if (buffer[0]) {
				P |= 0x777;
			}
			if (buffer[1]) {
				P |= (0x777 << 1);
			}
			if (buffer[2]) {
				P |= (0x777 << 4);
			}

			if (col == 0) {
