refactor write