Rewrite diagnostics code. This version has been tested and is working
properly.