It looks like the if/else condition is nearly identical aside from capturing the output. Would it make more sense to functionalize this and simply conditionally capture the output of the function call? That way you ensure behavior is identical regardless of whether the output is sent to console