cleaning up alg function