refactoring and batch insert