Fixes issues #510, and #156, as well as issues brought up in PR #373.