fix issue using popen with python 2.4, optimize code