Refine groups querying and add tests