Restructuring the main python code