Add missing include to common_funcs.h
Simplify buffer and fix deadlocks
Cleanups and robustness improvements
Finally fix clean-up properly.
Add SCOPE_EXIT
Fix log flushing