Instead of duplicating this, can you please move it into a function (and maybe add unit tests for it)