added aggregate support and refactored code