This is crying out for a context manager. Not in scope for this PR though.