end main alg