Possible fix for #538

I still need to write tests for this case to ensure I didn't break anything else, but the case described in issue #538 is fixed by this.