algorythm implemented with threads... it seems it works