Fix memory leak and script timeout.