Database connection and other functions