get rid of some junk, de globalize a couple of variables, use make_pair