It looks like this part and the section of code below this are very similar. Can you take a crack at refactoring this to remove some of the code duplication?