Move more class attributes used as global variables to local variables.
Fixed more memory leaks.