Fix average calculation bug