refactored/fixed file opening