Fixes for thread safety, issue #1811