improve insert function