fix 2 bugs with leaking filedescriptors