> And it's not so slow,

I wrote a small approximative benchmark for that runs the script with
different set configurations.
http://pastie.org/711915

It might be interesting to compare the runtime behaviour of your
script with other solutions.