added 2 functions to handle duplicated code within the average and median functions and removed the duplication