Improved (and complicated) groupCosts to group better

It’s not a fundamental solution to cost sorting, but it may be enough
for now. Fixes #4