Better mechanism for choosing aggregation function

We currently use hardcoded aggregation functions for targets that are wrapped in functions. This fails in certain circumstances.