Add option for debug output

And clean up source a little.