Refactoring openFile method