The checks for the text length and invalid parameters needed to be swapped, and the code could be easily simplified.