use safePairs() for table changing during loop

Use safePairs() to prevent lua errors that can occur if the table
pairs() is running on is updated while looping. safePairs() first
creates a copy of the table and uses that.