Multiple bugfixes and debug logging implemented.