Fixed error handling and corrected redirecting of stdin/out