Lots of changes (mainly handle cases like trivial groups and empty sets)