Jacob's patch to improve Close On Exit behaviour