Fixing a bug in the averaging algorithm