refactored the database inserts into a function