Improve grouping functionality

- Change _count on outer groups to count total docs in a inner group
- Add _group_count to count the groups in an inner group
- Pull out recursive merging into module method and add unit tests