optimization and fix for search_db function