Improved version of PR #86. Fixes #86.