You know what?  I think I just need to rewrite the whole section.  Give me a bit to mull it over and I'll put something together before I go to bed tonight.  I think what I have now was poorly organized, and nibbling at it isn't going to make it better.  (That and maybe I can avoid all these database locks I'm getting.)