backport of 02d3475d2f038a5fcf8dbbd2b8e1aff289106dbc from trunk, closes #599