Fixed not mallocing and freeing correctly