Refactor - DRY `instance` setting

Some code was noted by a comment to be dry'd up. Refactored the code into
local function with all dependencies passed in as arguments.