now algorythm works for every count of images