Same patch as before, but with a test case. Have a play with it and see if it makes sense.