Fixing grouping for maximums and minimums