changed query_database function