Fixed bug in length calculation of incoming data