line 46 - 52 is duplicated on 58 - 63. Can we make one function and call that in both places?