This code is ugly, but they had a gun to my head and made me do it.