Use apply to remove memory leak