added debug mode which writes out diagnostics data