Changed the median algorithm to a generic one