Fixed some bugs
Remove global variables and debug output
It works