cleanup and aggregation addition

moved what i could to functions and added an aggregate number calculation