Small fixes

This pull request contains fixes for some resource leak issues I've found throughout the code. It also eliminates unnecessary fflush() calls.