Reworked the query in the insert function