Updated debugging printf() calls to write to a temporary buffer so that multiple calls are not mixed up when writing