Fix chdir issue, add redirect loop for closing all threads