Reorganize python code