fix #1974 and #2413

This change fixes #1974 and #2413 

I have to figure out if there's a reasonable way to write tests for this.