simplify the code to remove pre-calculated length