The two operations are quite similar so the code should look similar as well and use shared functions.