Check index range using i < n, not i <= n