Updated algorithm to use average rather than sum