Fixed bug in close buffer function