fixed concurrency bug and added tests