Full fix for #318