Move error and debug output in to functions
Make debug output a config option