Bug fixes for database connection