There are a LOT of things that are very rough about this file, to say the least, but this code is temporary and will be deleted as soon as we've converted stage and prod... so I really don't think it's worth spending any time refactoring it, it's why I didn't, because I think it's kind of a big mess(even after I cleaned up some of it), but it works, so since it will be gone from the codebase in 1-2 weeks it doesn't really matter too much.