Backporting fixes from master