I would move the variables other than $count inside the if statement.  Also consider adding $t_ for all the temporary local variables you are using.