the algorithm looks good to me. Would be good to have @danberindei take a look as well.