A better example of using the "wait for stream to end" technique