fix race condition with changing directory

this one fixes issue #345