Move open file to directly before it is needed