changed perror too p_err and closes more file descriptors