Cumulative fixes for #2542 #2544

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