Fixing some memory leaks and build issues