added negative index check