Refactor debug mode

The `--debug` flag is currently handled in two places.

Also, errors seem to show up twice, once on STDERR and once on STDOUT.