altered maxIndex to be totalItems, changing code accordingly