Fix mean/variance calculation. Add explicit binary mode in calls to open().