Ensuring open file is closed