Unify insertion/update code