Keep ref to document in case it gets closed while opening it