refactoring writer to mirror the reader impl.