Reorganized Python code