fixing bugs with averaging