Algorithm second version