Simplified code from issue #83 pull request.