Another problem is that the posted code has lost most of the indentations of nested statements.
That also makes the code hard to read and understand.