Fix waiting for process to end