Refactored file opening code to automatically close the file after associated block is executed, omitting need for file.close