Generalize div swap into a function