code improvement: open writer in same method as where it is closed