fixing indexOutOfBoundsException