It was never meant to be clean. It's quick and dirty and it works, thats all. If I clean it up I'll submit it as a totally clean script to the UDF section and then I'll surely implement your idea.

But for now it works fine as is.