Issue 885

Fixes #885

This replaces a previous pull request (#946) which had some extra commits.