Fixes issue #510 and #156, as well as issues addressed in PR #373.