Second loop of the algo and a little optimized