Moved the conditional testing for the length in the while loop above so that if this is false then it will not evaluate the other conditions.