Generalized Algorithm