The repeated checks for `result` make the logic a tiny bit harder to follow -- maybe wrap this all in an `if (result) { ... } else { return null; }` to simplify?  (Then you can remove the ternary operators, etc.)