optimized algorythm