fix race condition with changing directory