fix a bug causing an IndexOutOfBoundException