Enhanced algorithm.