Changed mean computation to median

New PR with single commit to replace previous attempts.