Fix IndexOutOfBounds exception