File handles are now properly closed.

Fixes #899.