added guard for output stream and test