refactor code for opening file