Global flag caused race condition. Fixes #1