The whole pattern is duplicated a couple of times in this file. Lets move it out into a separate method.