Use std::min to simplify some bounds protection code.