The "wait next ms multiple" does NOT wait a specific amount of time, but waits until the ms counter is a multiple of the indicated value.
For example if you specify 500, it can take from 0 to 499ms until the wait completes the first time. If it is in a loop that can complete faster than 500ms, all later waits will complete 500ms after the previous, so the pacing will be exact. However, in your case you add other waits in an inner loop, throwing everything back out of sync.
For a more detailed explanation have a look at my answer in the following related post: