Changes to accommodate new schema, and some slight refactoring.