Merged patch suggested by @geoff-mci. Closes #961.