Refactoring to use the slice in the Write function