remove free() and malloc(), swap pointers instead
reorder lines