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 like to get away from using all these global
variables. Some certainly are necessary, but I feel like we can do
better and use parameters to make things more explicit.