Bug: Group distance was computed for only 100 of 161 groups. Fix: Forcing the database to compute all_groups before the loop seemed to solve the problem.