Some refactoring to avoid duplicate code in writing data to stream