Fix for #227 and #230

Here is an updated (again) pull request.