Refactor control flow in insert fn.