fix for issue #217; contains some more enhancements, too