refactoring open and writing pattern