Added talloc and fixed some bugs