Added possibility to group results by a given function