fix race condition and add tests