functionality of trim_groups() improved