Fixed bug due to incorrect handling of last grouping set