Fixed errors in averaging