Various bugfixes and debug statements