Added string length checking logic and modified comments