Avoiding concurrency issues. Fixes #83

(manually cherry picked from commit c48dc03de3beb1ccd160fa3a8e19eaa39ae6f06a)