Adding some checks to be  doubly sure we don't leak file handles.