Moved writer procedure to its own function