It's very much a hack, but it will do for now until I can rewrite the whole thing: it's not at all thread safe, but it is over eight years old and has survived a lot of bit-rot.