Comment 74 for bug 294712

Created an attachment (id=384058)
WIP 2

Ok, this is much better, thanks goes to bz.

Still need to write up some tests, after which this should be ready for review.