Fixed potential deadlock while waiting for process to finish