I think your approche is basicly correct. (I would go the same route.)
If it can be tweaked, depends on your exact code. If you would like to receive comments on it, post it here.
There may be alternatives whch are faster, but if they are easier to maintain?
Paul