fixed undefined variable $length bug