Fixed code to take TZ into account #319

I'm not extremely proud of the code efficiency, but I don't think I can do
better without a major refactoring of how things work.