backport fixes to 2-2-stable

This is my attempt at backporting fixes form master to 2-2-stable which seems to work.