Backport to Py2.6 and improvements in command-line script