Fixed memory leaks

Deallocated heap memory and file structures.