Again, all of these are almost identical code blocks, make them one reusable function.