Grouping rect elements correctly