Add debug mode and fix disconnect bug