Calculate correct mean, clean up code, and add a test for mean