finish algorithm