Factored out ugly code to ddemo_helpers.c