Memory management fixes
Now using emalloc/efree instead of malloc/free