Adding more safety code to prevent double-inserts