fixed various bugs with not freeing/closing resources on error. Also fixed Makefile to work at root of project