Fix temp counter

The code to increment the temp counter was in the outer loop, which had only two elements, rather than the inner loop, so it wasn't working.  This pull request fixes that.
It looks to me like email.close() was actually one level further out that it should have been (i.e. it email was opened inside a loop and closed outside that loop), so I fixed that as well.