Add output redirection and auto close functions