moved packet length check to the beginning