I cant think of a way to explain it better than the docstring. What makes this tricky is that it's used recursively and the recursive calls can be very different. Maybe splitting it in to two functions would make it a bit more clear, but my guess is not really.