Added check that index is within correct range.