This i smoe of a comment than an answer, but as code looks ugly in a comment I thought I would post as ...
stackoverflow.com