Update handler logic to avoid memory leak