added array bounds check