Properly close resources in finally block