Refactor writing of raw data