Issue217

This should fix issue #217, but more testing would be good before merging.