Fix segmentation fault on shutdown
Add debug logs