Debugging and logging fixes 

Lots of debugging fixes for running stuff under gdb. Make stderr output work properly.