It's OK if you don't deal with this in this PR, but there's now a whole lot of duplicated code in this file.  Can you add a comment reading

```js
  // XXX refactor duplicated code in this function
```