Correctly calculate the median