Take the first group instead of a random one.

This does assume that the primary group is always the last one, but on the
other hand it won't fail because no group is returned (as could be the case
with the former code).