added check for z==None out of averaging