modified database connection logic