re-implemented algorithm with a less recursive-limit sensitive one, improved previous algorithm a little both