backported fix for #512 to master