Major refactor of Python code for current production stack