Refactor batch into cleaner code