Improve subproces.Popen call
Refactor program flow for smaller functions (as per pylint suggestion)