refactor starting and stopping pexpect