improve insert logic