Fixes #683
Fix errors + improvements on bug #683 fix.
Merge pull request #684 from ldidry/issue-683

Fixes #683