Fixed median bug to improve stability