avoid overflowing in function avg