Tests and fix for #2970

Fixes #2970 with passing tests. I'm also pretty sure it fixes #2955 too, although I am going to run more tests on that to find out.