Pct 697  agent running but down in pct

From line 134 to here, factor out this code into a separate function and unit test it.