Fix #671

With help from @dylex.

@garyb @dylex @joneshf Could you please review?