added more tracing to track down the segfault.
made some debug output conditional.