used regex for invalid check and removed to_i method