Backported fix for #362.