Backport fixes to 1.8

backport #4020, #4051, #4094, #4115, #4124, #4141, #4144, #4163 to 1.8.x