These two blocks are very similar. I think we can refactor this code to reduce duplication.