Fix query counting.
Make lazy importing code thread-safe.