removed a memory leak and an overwrite on an array