replaced std::min with optimized version in loop