CRM-15376 - Porting fixes from https://github.com/civicrm/civicrm-core/pull/4233...