add some tests and complete refactoring of the database connection