Refactor: General clean up

Move the DOM stuff into their own functions,
and focus (a little bit) the work done by each
function, to limit side effects.