Restore prevgroup and nextgroup

The `prevgroup` and `nextgroup` commands have been broken by a recent commit.
Unit tests also fail.

The logic for group selection is poor, redundant, and does not handle the case where both `skip_empty` and `skip_managed` are False.