Fix concurrency problem, fix tests