changed buffer length to buf.length