Refactoring to query for the entities before adding them