This logic is the same in both braches of the if, move it outside and delete the else