Cherry-pick degasus's patch, fixing everything.