refactor batch function