Problems executing custom aggregation function on large datasets