Fixed bug in median calculation