refactored stream writer