better subprocess management, refactor