switched all to non-blocking mode; fixed write_nonblock