Fix possible IndexOutOfBoundException