There's a lot of `.find`, `.attr` and `.children` calls here, perhaps there's a chance to refactor into methods like `child(n)`, `hasTwoChildren` and `hasTwoGrandChildren`. I'm wary of names like `hasTwoGrandChildren` but something like `hasGrandChildren(n)` can be confusing.