Since the bodies of each if statement are similar, a small helper function could make the code here a wee-bit smaller as well.