Added result grouping functionality and tests