Added array bounds check