Extracted the insert query into a function.