This fixes #1378. We should refactor the whole thing, but for now it works.
Merge pull request #1379 from capooti/issue-1378

This fixes #1378.