Added better handling for invalid streams