partly refactored write