Fix to JENKINS-10730 plus appears to fix memory leaks

Tested informally for a few weeks and no memory leaks