Fixes for CRM-15247 and CRM-15248 back-ported from 4.4.