rewrote insert function