Further code cleanup

Got rid of document.write, re-using the existing code instead.