Optimized part of the recursion

Now only calculates log(n) once. Is about twice as fast