Another optimization: use running totals.