This is potentially generally useful. Should we make a utility function in `io` that does this?