Fixed incorrect condition on index check

was leading to segmentation faults