modified expansion_search with robert: moved the variable loop inside the expansion_search function