Fixed the median function so that it takes the average if necessary.