Finish algorithm