Break out common code for subprocess handling