Added safer initialization of stream and tests.