Full fix for CRM-11696

Backport of https://fisheye2.atlassian.com/changelog/CiviCRM?cs=44917