Improved some debug statements.
Added some workarounds for a bug that still needs to be investigated.