Better debugging code
Fixes for some errors