REPLACED median function