Refactor and docs

Only using one global variable now, everything else is scoped to a self
executing function