Convert main() function to python idiom

It allows the module to be used either as a library module, or as a standalone script