Moves some utility code out of the index.html

Also organize and document the util code a bit.