added better flag-error check in the output function and other ish