Improvements to aggregation code and fucntion library