Restructure the database access and api.