Tests and fix for #666.

Closes #666.