Minor fixes to improve thread-safety