modularity rewritten to use generators/itertools. quite a speedup