lots of refactoring to accomodate the new db schema