Backports for the 4.0.4 release

Backported PR's:
* #665  
* #668 

@deiferni please have a look ...