added bad index check