Refactor and fix resource leaks.
Fix compiler complaints.
Fix display of open files.
Cleanup the code to prevent this bug from happening again.