     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)
+        while ((b < B) && (seq_start+lengths[b]) <= t)
         {
            seq_start += lengths[b];
            b += 1;

        // 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)
        {
           seq_start += lengths[b];
           b += 1;

    float two = 2.;
    for (int i = _loop_start; i < N; i += _loop_stride)
    {
        float x = X[i];
        if (x >= threshold)
        {
            dX[i] = dY[i];
        } else
        {
            float exp_x = exp(x);
            float exp_2x = exp(2*x);
            float exp_3x = exp(3*x);

            float omega = (4. * (x+1)) + (4 * exp_2x) + exp_3x + exp_x * (4.*x+6);
            float delta = 2 * exp_x + exp_2x + 2;
            dX[i] = dY[i] * ((exp_x * omega) / pow(delta, two));
        }
    }
}

