As discussed on irc, I think the functions here should be wrapped in some sort of object, but also as discussed, we'll leave the refactoring to later as it is all internal to the file.