4th algorithm improvment