condense copy-paste code into new function "init()"

There are certainly better names for a function, but I'm not feeling
terribly creative at the moment.

Ideally, I would life to get away from passing all these global
variables. Some certainly are necessary, but I feel like we can do
better and use parameters to make things more explicit.