Add initial thread-safety and some tests.