Replaced search and assignment by two parts algorithm