void CleanupOutput(char *str)
{
    char *s, *t;
    int period = 0;
    s = t = str;
    // Collapse multiple white space (' ' and '\n') to one.  Remove trailing space.
    while ( *s ) {
        if ( *s == '\n' ) {*s = ' '; }
        if ( (*s == ' ') && ( s[1] == ' ' || s[1] == '\n' || s[1] == 0 ) ) { s++;}
        else { *t++ = *s++; }
    }
    *t = 0;

    // Optimize format of numbers:
    s = t = str;
    while ( *s ) {
        if ( *s == '.' ) { period = 1; *t++ = *s++; }
        else if ( isdigit(*s) ) { *t++ = *s++; }
        else if ( period ) {
          while ( t > str && t[-1] == '0' ) { t--; }
            if ( t > str && t[-1] == '.' ) {
                t--;
                // Handle case that number is .000, not e.g. 9.000
                if (t > str && ! isdigit(t[-1]) ) {
                  *t++ = '0';
                }
            }
            period = 0; *t++ = *s++;
        }
        else {
            period = 0; *t++ = *s++;
        }
    }
    *t = 0;

    // Collapse '-0' to '0'
    s = t = str;
    while ( *s ) {
        if ( *s == '-' && s[1] == '0' && s[2] == ' ' ) { s++; }
        else *t++ = *s++;
    }
    *t = 0;
}