Remove needless check for the window object. Use passed in variables for the document instead of the global document object