Restructured the code. It now runs.