Memory leak fix and Makefile update