factorize with counting and group_sorting without it