Algorithms ported to js from c