refined js for recursion