So, I think I've managed to fix this. Unfortunately, I don't actually have hardware to hand so that I can test it directly. I've written a test script which exercises as many cases as I can think of, and they all pass now, but the real test will be trying this on actual hardware. As such, please try tomorrow's daily build, and if it still goes wrong then please do let me know and reopen this bug.
So, I think I've managed to fix this. Unfortunately, I don't actually have hardware to hand so that I can test it directly. I've written a test script which exercises as many cases as I can think of, and they all pass now, but the real test will be trying this on actual hardware. As such, please try tomorrow's daily build, and if it still goes wrong then please do let me know and reopen this bug.