handle possible IndexOutOfBoundsException