Compartmentalized

Moved several sections of code into functions so they can be reused some
other time.