Various bugfixes and debug outputs