More refactoring on Python code