algorithm is almost finished