This block of code is repeated a *lot*. I would suggest abstracting into a function.