changed ensemble average code