improved handling of open streams and files with finally block