Added some tests, fixed race condition.