backported fix for #562 to master