refactorin insert function