This current version was tested, it works.

There are lots of alternative ways to handle this properly, this seems one of the better options imho.

But, we're bikeshedding here... @JensTimmerman: OK to merge according to you?