Implemented group demoting by means of a disjoint-set datastructure. Fixes #1194