remaining length logic