         // the gradient of the maxes. In this loop, we're getting the gradient
         // of a single sequence item, t. We need to know the sequence index,
         // b.
-        while ((b < B) && (seq_start+lengths[b]) < t)
+        while ((b < B) && (seq_start+lengths[b]) <= t)
         {
            seq_start += lengths[b];
            b += 1;

    for (int t = _loop_start; t < T; t += _loop_stride)
    {
        // Find the sequence item we're working on
        while ((b < B) && (seq_start+lengths[b]) < t)
        {
           seq_start += lengths[b];
           b += 1;

        // Find the sequence item we're working on
        const float* X_t = X;
        for (int i=0; i < b; ++i) {
	        X_t += lengths[i] * O;
        }
        // Each invocation of the kernel maxes one sequence.
        // Start by assuming maxes are the first element.
