fixing issue with files not being closed after callback is fired