Back-porting fixes from master