Use reduce in median calculation