Bugfixes and debug logging improvements

Fixes for bugs and logging