fixed some typos and added some memory management