Cumulative fixes for #2542 #2544 for V3.9 (same as the ones for 4.0 to master)

Cumulative fixes for #2542 #2544
Same fixes than for 4.0 to master already committed.