Changed to using thread-safe methods. Thanks to super_tycoon