finish the solving algorithm