Optimization broken in current develop

Fixed by flushing and closing output stream.

Fixes #259.