udf: move calculating of nr_groups into helper function