Refactor re-used logic into utils.