Ensure all open file handles and streams are properly closed