Base alg, wrested from my other proj