Integrate changes from #1841.