fixed some non flushing and added well-behaved stream closing (fixes #31)