refactor main.py, flesh out functions from main function

@stdweird, @wpoely86: please review?

The diff is going to look a bit messy, maybe you want to look at the actual new `main.py` instead...
ah, this part was not supposed to go in yet, it doesn't belong here... I'll get it kicked out.

What's the problem with `OrderedDict` exactly here?