modify the codes to get clear in logic and add limitation of input