Fix for issue #493. Tests do all work, need some more checking if this really works.