Corrected memory leaks and cleaned up code and makefile