Tests and fix for #2970

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