fixed memory and file descriptors leak