Lots of changes

Restructured things to create more flexibility.