Refactor common code into private helper. As a bonus re-format a small if clause. Preparation for #358 on github.