Moving exitstatus assignment up inside the thread to ensure it is set correctly

removed close and unlink on tempfile as it is not needed