refactored aggregate functions