Actually, the rows only need here to be added if they are grouped:

replace
} else{
with
} if (grouped) {