Changed to explicitly close file handles