I think I managed to condense a lot of your branching into a dictionary that can be passed as kwargs to Popen. This way we can reduce the number of places we make calls to subprocess.Popen()