io MAINTENANCE minor enhancements
diff --git a/src/io.c b/src/io.c
index e2ba7de..fab1240 100644
--- a/src/io.c
+++ b/src/io.c
@@ -37,7 +37,7 @@
 static ssize_t
 nc_read(struct nc_session *session, char *buf, size_t count, uint16_t *read_timeout)
 {
-    size_t size = 0;
+    size_t readd = 0;
     ssize_t r = -1;
     uint16_t sleep_count = 0;
 
@@ -52,14 +52,14 @@
         return 0;
     }
 
-    while (count) {
+    do {
         switch (session->ti_type) {
         case NC_TI_NONE:
             return 0;
 
         case NC_TI_FD:
             /* read via standard file descriptor */
-            r = read(session->ti.fd.in, &(buf[size]), count);
+            r = read(session->ti.fd.in, buf + readd, count - readd);
             if (r < 0) {
                 if (errno == EAGAIN) {
                     r = 0;
@@ -86,7 +86,7 @@
 #ifdef NC_ENABLED_SSH
         case NC_TI_LIBSSH:
             /* read via libssh */
-            r = ssh_channel_read(session->ti.libssh.channel, &(buf[size]), count, 0);
+            r = ssh_channel_read(session->ti.libssh.channel, buf + readd, count - readd, 0);
             if (r == SSH_AGAIN) {
                 r = 0;
                 break;
@@ -111,7 +111,7 @@
 #ifdef NC_ENABLED_TLS
         case NC_TI_OPENSSL:
             /* read via OpenSSL */
-            r = SSL_read(session->ti.tls, &(buf[size]), count);
+            r = SSL_read(session->ti.tls, buf + readd, count - readd);
             if (r <= 0) {
                 int x;
                 switch (x = SSL_get_error(session->ti.tls, r)) {
@@ -149,14 +149,13 @@
                 session->term_reason = NC_SESSION_TERM_OTHER;
                 return -1;
             }
-            continue;
         }
 
-        size += r;
-        count -= r;
-    }
+        readd += r;
+    } while (readd < count);
+    buf[count] = '\0';
 
-    return (ssize_t)size;
+    return (ssize_t)readd;
 }
 
 static ssize_t
@@ -336,7 +335,7 @@
 
         break;
     }
-    DBG("Session %u: received message:\n%s", session->id, msg);
+    DBG("Session %u: received message:\n%s\n", session->id, msg);
 
     /* build XML tree */
     *data = lyxml_parse_mem(session->ctx, msg, 0);
@@ -584,15 +583,15 @@
         return -1;
     }
 
-    DBG("Session %u: sending message:\n%.*s", session->id, count, buf);
+    DBG("Session %u: sending message:\n%.*s\n", session->id, count, buf);
 
-    while (written < count) {
+    do {
         switch (session->ti_type) {
         case NC_TI_NONE:
             return -1;
 
         case NC_TI_FD:
-            c = write(session->ti.fd.out, &((char *)buf)[written], count - written);
+            c = write(session->ti.fd.out, (char *)(buf + written), count - written);
             if (c < 0) {
                 ERR("Session %u: socket error (%s).", session->id, strerror(errno));
                 return -1;
@@ -611,7 +610,7 @@
                 session->term_reason = NC_SESSION_TERM_DROPPED;
                 return -1;
             }
-            c = ssh_channel_write(session->ti.libssh.channel, &((char *)buf)[written], count - written);
+            c = ssh_channel_write(session->ti.libssh.channel, (char *)(buf + written), count - written);
             if ((c == SSH_ERROR) || (c == -1)) {
                 ERR("Session %u: SSH channel write failed.", session->id);
                 return -1;
@@ -622,7 +621,7 @@
         unsigned long e;
 
         case NC_TI_OPENSSL:
-            c = SSL_write(session->ti.tls, &((char *)buf)[written], count - written);
+            c = SSL_write(session->ti.tls, (char *)(buf + written), count - written);
             if (c < 1) {
                 switch ((e = SSL_get_error(session->ti.tls, c))) {
                 case SSL_ERROR_ZERO_RETURN:
@@ -652,7 +651,7 @@
         }
 
         written += c;
-    }
+    } while (written < count);
 
     return written;
 }