refatored avg