rewrite the ul generate algorithm