Fixed checking of index limits.