 
 		CUDA_CHECK(cudaStreamSynchronize(stream));
 
-		if (static_cast<size_t>(std::count(found.begin(), found.end(), 1)) == found.size()) break;
+		if (N == foundCount) break;
 
 		// preserve starting position for those grid points that have value
 


		curLevel.Value(curLevel.Value() - 1);

		size_t foundCount = std::count(found.begin(), found.end(), 1);

		if (foundCount == found.size()) break;
	}

	CUDA_CHECK(cudaFree(h_prevP->values));

		double P = d_P.values[idx];
		double RH = d_RH.values[idx];

		if (P < 600.)
		{
			// Cut search if reach level 600hPa

			// Linearly interpolate temperature and humidity values to 600hPa, to check
			// if highest theta e is found there

			T = numerical_functions::interpolation::Linear(600., P, d_prevP.values[idx], T, d_prevT.values[idx]);
			RH = numerical_functions::interpolation::Linear(600., P, d_prevP.values[idx], RH, d_prevRH.values[idx]);

			d_found[idx] = 1;  // Make sure this is the last time we access this grid point
			P = 600.;
		}

		double TD = metutil::DewPointFromRH_(T, RH);

		double& refThetaE = d_maxThetaE[idx];
		double ThetaE = metutil::smarttool::ThetaE_(T, RH, P * 100);

		if (ThetaE >= refThetaE)
		{
			refThetaE = ThetaE;
			d_Tresult[idx] = T;
			d_TDresult[idx] = TD;
			d_Presult[idx] = P;
		}
	}
}
