updated function for writing to database