Needed to not use the totalElements variable here because that was being used in the containing loop.