fixing memory issue with mean()