Added functions and closures.
Changed to document.write for uniformity