General version of the frequentist gmeans code