Extensive refactoring; adding version field to DB.