Backported fixes from master