I'm glad you found a solution.
Here are two similar scripts that we developed and highly optimized. It was published about a year ago:
For more information, see the wiki Searching the Forum for Answers section.