I suggest taking your script and cutting it into chunks. Then you might be able to see where the lag is.

By the way, if you use the script above, I modified it, as I saw some glaring bugs in it, and also removed the persistence. I rarely ever see a reason to use it, because when you log in, they get mapped anyway.