length check added, small changes