The current script tries to remove wait in pwd not outdir and so wait loop will never exit.
wait
pwd
outdir
merged