refactored stream writer with retry capability