updated to use hadley's sample code for mean