refactored so that db is not initialized each time