Fix memory leaks and implement getname wrapper