Second update with doc fix

This is a second version where I also found an assumption of document.location where it should check to see if that exists before it uses it.