updated alg a little bit, added some comments