Fix memory leaks & use correct "delete" for arrays