Extract from PR 392, just the fix for ticket 330