Added optimized algo