Note that my patch is just a proof of concept pointing at where the issue lies. I hope it helps someone else come up with a proper fix.