max was converted to zero based twice. Removed -1 in the switch up call that prevented the stream from switching to the highest index.