Added some tentative multithreading safety
Added a few tests.