Cherry picked bug fix from @aaslun