added Makefile
fixed some memory leaks, improved memory management