fix average calculation logic