Improved error handling in check_output