Add misc debug stuff
Some refactoring