added a more accurate way of calculating packet length