Instantiate the $html variable before trying to append to it

Per #145.
Correct a counting error

It was causing errors on the last iteration through the loop. Per #145.
Reverse the conditionals to suppress errors

Per #145.
Make sure variable is set before using it

Per #145.
Make sure the variable is set before using it

Per #145.
Instantiate the $text variable before trying to append to it

Per #145.