Fixing the ArrayOutOfBounds error with unexpected result