Argh, it's ugly but functionally correct. I'll fix it.