Fixed bug of not closing output bag.