Backport 216d8cb and c2465b6 to 1.7.x