AgrDatabase and some refactoring