Replace subprocess.check_output with subprocess.Popen to make the script compatible with Python 2.6
Refactored the code into functions
Made the code pass pylint/pyflakes/pep8