Ok, I think I have done this more intelligently now in conjunction with several other changes to this code, but feel free to comment otherwise.