Reorganize and comment code to be clearer about how things are set up