extract detect_subshell_pid, cleaning code