Optimization broken in develop (pull request)

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