If we refactored this whole thing into a class, then we might not need the nasty "global" stuff...? Any thoughts there?