some code cleanup to avoid warnings and insure filehandles are properly closed, etc