Added group by support and fixed spending.