Fixed bug for array index out of bounds