fix of possible memory leak + minimize batch loop