The performance issues seems to be mitigated well enough by adding sleep statements, although I'd prefer a better solution. Also, I started cleaning up the code and optimizing the query performance some more.