fixed median function