main alg