fix race conditions