some changes to avoid memory leaks