Refactored common Python code