updated write logic for easier readability