Algorithm almost finished