Fixed median bug to improve stability
Merge