fix #436
I learned some things: improve thread safety
Merge branch 'pr439'