optimized algorithm.