Fixing an indexOutOfBounds error