Refactor file opening