Bugfix: close stream after writing