Backport fixes

backport #4094, #4051, #4124, #4020, #4141 to 1.7.x