added length check and tests