Refactored stdout redirection into util module