It's not important, but it appears you can test for chatType mismatch as the first step in this loop.