A lot of this stuff looks like it should be in a utility library (the main function too), otherwise it's getting copy/pasted everywhere.