Add database connection and refactore