modified insert function