Fix file open logic