issue #540: merged patch by @guidoreina, added test